//
//  HomeNewcomerCell.swift
//  HengYouCai
//
//  Created by lwj on 2017/11/9.
//  Copyright © 2017年 lwj. All rights reserved.
//

import UIKit
import SnapKit
class HomeNewcomerCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    /// 活动标签
    var tagsView : JCWActiveTagView!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = UIColor.white
        initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /// 明星标
    public func wl_configurationCellWithStar(_ modelTemp: Any?, _ listView: UIScrollView, _ indexPath: IndexPath) {
//        newImg.image = UIImage(named: "home_label")
        let model : ProjectModel = modelTemp as! ProjectModel
        //标签图片
        let imageUrl = URL(string: model.typeIcon)
//        newImg.kf.setImage(with: ImageResource(downloadURL: imageUrl!))
//        newImg.kf_setImage(with: ImageResource(downloadURL: imageUrl!))
        
        title.text = model.title
        if model.cashRateShow == "1"{
            self.profitLab.attributedText = handlerProfit(model.profit, model.cashRateProfit)
        }else{
            self.profitLab.attributedText = handlerProfit(model.profit, model.profitFloat)
        }
        let inBeginAttStr = handlerInvest(String(format: "%@  ", model.plstimeLimitLabel) , (model.plstimeLimitValue!), (model.plstimeLimitType == "0" ? "天" : "月"))
        var surplusAmountAttStr = NSMutableAttributedString()
        let surplusAmount = model.surplusAmount.cGFloat
        if  surplusAmount > 9999 {
            surplusAmountAttStr = handlerInvest(String(format: "%@  ", model.investBeginLabel), String(format: "%.2f", surplusAmount/10000.0), "万元")
        }else{
            surplusAmountAttStr = handlerInvest(String(format: "%@  ", model.investBeginLabel), String(format: "%.0f", surplusAmount), "元")
        }
        inBeginAttStr.append(NSAttributedString(string: "    "))
        inBeginAttStr.append(surplusAmountAttStr)
//        self.investBeginLab.text = "投资期限  " + (model.plstimeLimitType == "0" ? "\(model.plstimeLimitValue!)天" : "\(model.plstimeLimitValue!)月") + "    " + "剩余可投  " + money
        self.investBeginLab.attributedText =  inBeginAttStr
        self.tagsView.tagsStr(model.tags)
        tagsView.snp.updateConstraints { (make) in
            make.centerX.equalToSuperview().offset(-tagsView.totoalWidth/2)
        }
        panicBuyingBtn.setTitle(model.buttonLabel, for: .normal)
        self.titleLab.text = model.profitLabel //"历史年化收益率"
    }
    /// 新手标
    public func wl_configurationCellWithNew(_ modelTemp: Any?, _ listView: UIScrollView, _ indexPath: IndexPath) {
//        newImg.image = UIImage(named: "home_newlabel")
        
    
        let model : ProjectModel = modelTemp as! ProjectModel
        title.text = model.title
        //标签图片
        let imageUrl = URL(string: model.typeIcon)
//        newImg.kf.setImage(with: ImageResource(downloadURL: imageUrl!))
//        newImg.kf_setImage(with: ImageResource(downloadURL: imageUrl!))
        
        self.titleLab.text = model.profitLabel //"历史年化收益率"
        self.profitLab.attributedText = handlerProfit(model.profit, model.profitFloat)
        let inBeginAttStr = handlerInvest(String(format: "%@  ", model.investBeginLabel),model.investBegin, "元")
        let investBeginAttStr = handlerInvest(String(format: "%@  ", model.plstimeLimitLabel), model.plstimeLimitValue, (model.plstimeLimitType == "0" ? "天" : "月"))
        inBeginAttStr.append(NSAttributedString(string: "      "))
        inBeginAttStr.append(investBeginAttStr)
        self.investBeginLab.attributedText =  inBeginAttStr

//        self.investBeginLab.text = "投资期限  " + (model.plstimeLimitType == "0" ? "\(model.plstimeLimitValue!)天" : "\(model.plstimeLimitValue!)月") + "    " + "起投金额  " + "\(model.investBegin.cGInt)元"
        self.tagsView.tagsStr(model.tags)
        self.tagsView.snp.updateConstraints { (make) in
            make.centerX.equalToSuperview().offset(-tagsView.totoalWidth/2)
        }
         panicBuyingBtn.setTitle(model.buttonLabel, for: .normal)
    }
    /// 处理历史年化收益率的富文本
    func handlerProfit(_ str1: String, _ str2: String) -> NSMutableAttributedString {
        
        let str11 = String(format: "%.2f", str1.cGFloat * 100)
        
        let temp = NSMutableAttributedString(string: str11, attributes: [NSFontAttributeName: Font_WL(33)])
        temp.append(NSAttributedString(string: "%", attributes: [NSFontAttributeName: Font_WL(14)]))
        if str2.isEmpty {
            return temp
        }else{
            let str22 = String(format: "%.2f", str2.cGFloat * 100)
            temp.append(NSAttributedString(string: "+\(str22)%", attributes: [NSFontAttributeName: Font_WL(14)]))
            return temp
        }
    }
    /// 处理投资期限等的富文本
    func handlerInvest(_ str1: String, _ str2: String,_ str3: String) -> NSMutableAttributedString {
        
        
        let temp = NSMutableAttributedString(string: str1, attributes: [NSForegroundColorAttributeName: Normal_888Color])
        temp.append(NSAttributedString(string: str2, attributes: [NSForegroundColorAttributeName: Normal_ThemeColor]))
        temp.append(NSAttributedString(string: str3, attributes: [NSForegroundColorAttributeName: Normal_888Color]))
        return temp
    }
    func initUI() {
        
        /// 背景view
        let edage = UIEdgeInsetsMake(7, 15, 7, 15)
        self.addSubview(self.bgView)
        _ = bgView.sd_layout().spaceToSuperView(edage)
        
        
        bgView.layer.shadowColor = ColorFromRGBHex_WL(0xcccccc).cgColor

        bgView.layer.shadowOpacity = 1
        bgView.layer.shadowOffset = CGSize(width: 0, height: 0)
        
//        bgView.addSubview(newImg)
//        newImg.snp.makeConstraints { (make) in
//            make.top.equalToSuperview()
//            make.left.equalToSuperview().offset(14)
//            make.width.equalTo(33)
//            make.height.equalTo(22.5)
//        }
        /// title
        bgView.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.centerX.equalToSuperview()
        }
        /// 历史年化收益率值
        bgView.addSubview(self.profitLab)
        profitLab.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(title.snp.bottom).offset(5)
        }
        
        /// 历史年化收益率
        bgView.addSubview(self.titleLab)
        titleLab.snp.makeConstraints { (make) in
            make.top.equalTo(profitLab.snp.bottom).offset(2)
            make.centerX.equalToSuperview()
        }
        
        /// 标签
        tagsView = JCWActiveTagView(ColorFromRGBHex_WL(0xece2cd), false, "")
        bgView.addSubview(tagsView)
        tagsView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLab.snp.bottom).offset(2)
            make.height.equalTo(20)
        }
        /// 立即投资
        bgView.addSubview(self.panicBuyingBtn)
        _ = panicBuyingBtn.sd_layout()
            .bottomSpaceToView(bgView,12)?
            .centerXEqualToView(bgView)?
            .heightIs(40)?
            .widthIs(240.aw_wl)

      
        /// 起投金额 /// 投资期限
        bgView.addSubview(self.investBeginLab)
        investBeginLab.snp.makeConstraints { (make) in
            make.bottom.equalTo(panicBuyingBtn.snp.top).offset(-10)
            make.centerX.equalToSuperview()
        }
        
        let lineView = UIView()
        lineView.backgroundColor = Normal_CCCColor
        bgView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalTo(investBeginLab.snp.top).offset(-10)
            make.height.equalTo(0.5)
            make.right.equalTo(-25)
            make.left.equalTo(25)
        }
        
        
      
    }
    
    lazy var bgView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        temp.layer.cornerRadius = 4
//        temp.clipsToBounds = true
        return temp
    }()

//    lazy var newImg: UIImageView = {
//        let temp = UIImageView()
////        temp.image = UIImage(named: "home_newlabel")
//        return temp
//    }()
    /// 标题
    lazy var title :UILabel = {
        let title = UILabel()
        title.textAlignment = .center
        title.textColor = Normal_333Color
        title.font = Font_Auto(15)
        title.text = ""
        return title
    }()
    /// 历史年化收益率
    lazy var profitLab :UILabel = {
        let profit = UILabel()
        profit.textColor = Normal_ThemeColor
        profit.font = Font_WL(33)
        profit.text = ""
        profit.textAlignment = .center
        return profit
    }()
    
    /// 标题
    lazy var titleLab :UILabel = {
        let title = UILabel()
        title.textAlignment = .center
        title.textColor = Normal_666Color
        title.font = Font_Auto(11)
//        title.text = "历史年化收益率"
        return title
    }()
    /// 起投金额
    lazy var investBeginLab :UILabel = {
        let investBegin = UILabel()
        investBegin.textAlignment = .center
        investBegin.textColor = Normal_666Color
        investBegin.font = Font_Auto(11)
        investBegin.text = ""
        return investBegin
    }()
    
    /// 抢购按钮
    lazy var panicBuyingBtn :UIButton = {
        let panicBuying = UIButton(type: UIButtonType.custom)
        panicBuying.setBackgroundImage(UIImage(named : "btn"), for: .normal)
        panicBuying.setBackgroundImage(UIImage(named : "btn"), for: .highlighted)
        panicBuying.isUserInteractionEnabled = false
//        panicBuying.setTitle("立即抢购", for: .normal)
        panicBuying.setTitleColor(UIColor.white, for: .normal)
        panicBuying.titleLabel?.font = Font_WL(14)
        panicBuying.backgroundColor = UIColor.white
        return panicBuying
    }()
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
}

